package com.cx.bank.servlet;

import com.cx.bank.exception.InvalidDepositException;
import com.cx.bank.handler.MyInVocationHandler;
import com.cx.bank.manager.IManager;
import com.cx.bank.manager.ManagerImpl;
import com.cx.bank.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

/**
 * 取款
 * @author jxau_scf
 * @date 2021/8/9 - 12:00
 */
@WebServlet(urlPatterns = "/user/withdrawalsMoney")
public class WithdrawalsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userId = req.getParameter("userId");
        String withdrawalsMoney = req.getParameter("withdrawalsMoney");
        //获取代理对象，方便使用日志
        IManager manager = new ManagerImpl();
        User user = null;
        try {
            user = manager.findUserByUserId(Integer.valueOf(userId));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        MyInVocationHandler handler = new MyInVocationHandler();
        handler.setUserName(user.getUserName());
        handler.setTarget(manager);
        IManager proxy = (IManager)handler.getProxy();

        try {
            boolean withdrawalsStatus = proxy.withdrawals(user.getUserName(), Double.valueOf(withdrawalsMoney));
            //返回transferStatus给前端
            PrintWriter writer = resp.getWriter();
            writer.println(withdrawalsStatus);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InvalidDepositException e) {
            e.printStackTrace();
        }

    }
}
